package com.wlf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wlf.entity.ElasticSearchRequest;
import com.wlf.service.IElasticSearchService;
import org.apache.xalan.lib.sql.XConnection;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @author 作者 危凌峰:
 * ES 增删改查
* @version 创建时间：2021年4月28日 上午9:39:49
*/
@Service
public class ElasticSearchServiceImpl implements IElasticSearchService {

	private static Logger logger = LoggerFactory.getLogger(ElasticSearchServiceImpl.class);


	@Autowired
	private RestHighLevelClient restHighLevelClient;

	/**
	 * 保存
	 * @param request
	 * @return
	 */
	@Override
	public IndexResponse save(ElasticSearchRequest<?> request) throws Exception{

		IndexRequest indexRequest = new IndexRequest(request.getIndex());
		indexRequest.source(request.getBody(), XContentType.JSON);
		indexRequest.timeout(TimeValue.timeValueSeconds(30));
		IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

		return indexResponse;
	}

	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@Override
	public DeleteResponse delete(ElasticSearchRequest<?> request) throws IOException {

		DeleteRequest deleteRequest = new DeleteRequest(request.getIndex(),request.getId());
		DeleteResponse deleteResponse =restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);

		return deleteResponse;
	}

	/**
	 * 局部更新
	 * @param request
	 * @return
	 */
	@Override
	public UpdateResponse partialUpdate(ElasticSearchRequest<?> request) throws Exception{

		UpdateRequest updateRequest = new UpdateRequest(request.getIndex(),request.getId());
		Map<String,Object> jsonMap = (Map<String, Object>) request.getBody();
		updateRequest.doc(jsonMap);
		updateRequest.timeout(TimeValue.timeValueSeconds(30));
		//设置重试次数
		updateRequest.retryOnConflict(1);
		//执行
		UpdateResponse response = restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);

		return response;
	}

	/**
	 * 全量更新
	 * @param request
	 * @return
	 */
	@Override
	public IndexResponse update(ElasticSearchRequest<?> request) throws IOException{

		//构建请求
		IndexRequest indexRequest = new IndexRequest(request.getIndex());
		indexRequest.id(request.getId());

		indexRequest.source(request.getBody(),XContentType.JSON);

		indexRequest.timeout(TimeValue.timeValueSeconds(30));

		IndexResponse response = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);

		return response;
	}

	/**
	 * 查询
	 * @param request
	 * @return
	 */
	@Override
	public GetResponse query(ElasticSearchRequest<?> request) throws Exception{

		//构建请求
		GetRequest getRequest = new GetRequest(request.getIndex(),request.getId());
		//设置请求元字段
		String[] includes = Strings.EMPTY_ARRAY;
		String[] excludes= Strings.EMPTY_ARRAY;
		FetchSourceContext fetchSourceContext = new FetchSourceContext(true,includes,excludes);
		getRequest.fetchSourceContext(fetchSourceContext);
		//执行
		GetResponse response=restHighLevelClient.get(getRequest,RequestOptions.DEFAULT);
		return response;
	}
}
